<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\App;

use think\exception\ValidateException;
use think\facade\Log;

use util\Excel;
use util\Token;
use util\Upload;

use think\facade\Db;
use think\cache\driver\Redis;
/**
 * Ordercorrect控制器
 */
class Ordersource extends AdminBase
{

    protected $model = null;

    protected $validate = null;

    protected $relationSearch = false;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Ordersource();

        $this->validate = \app\admin\validate\Ordersource::class;
    }

    /**
     * 列表
     */
    public function index()
    {
        list($limit, $where, $sortArr) = $this->buildTableParames();

        $list = $this->model
            ->where($where)
            ->order('date desc')
            ->paginate($limit);

        $data = [
            'code' => 1,
            'msg' => '',
            'count' => $list->total(),
            'data' => $list->items(),
        ];

        return json($data);
    }
    
    /**
    * 方法详情
    * @param array params
    */
    public function download_dispute()
    {
        $redis = new Redis();
        $date = input('date');
        $b_time = strtotime($date);
        $e_time = strtotime(date("Y-m-d", strtotime("$date +1 month")));
        $sql = "
            select
            created_at,
            site_id,
            name,
            total_price,
            currency,
            gateway,
            financial_status,
            fulfillment_status,
            tags
            from qu_order 
            WHERE created_at >= ".$b_time."
            AND created_at < ".$e_time."
            AND disputestatus = 'normal'
            ORDER BY created_at DESC";
            $join_datas = Db::query($sql);
            $list = array();
            $num = 0;
            if(!empty($join_datas)){
                foreach ($join_datas as $k=>$item) {
                    $item['created_at'] = date('Y-m-d', $item['created_at']);

                    $site_name = $redis->get('download_sales-' . $item['site_id']);
                    if(!empty($site_name)){
                        $item['site_id'] = $site_name;
                    }else{
                        $site = table('site')
                        ->where('id',$item['site_id'])
                        ->find();
                        if(!empty($site)){
                            $site_name = $site['site_name'];
                            $redis->set('download_sales-' . $item['site_id'], $site_name, 60 * 60);
                            $item['site_id'] = $site_name;
                        }
                    }  
                    $num += 1; 
                    $list[] = $item;
                } 
            }
        
        $header = array();
        $header[] = ['日期', 'created_at'];
        $header[] = ['店铺', 'site_id'];
        $header[] = ['订单号', 'name'];
        $header[] = ['原销售额$', 'total_price'];
        $header[] = ['币种', 'currency'];
        $header[] = ['支付渠道', 'gateway'];
        $header[] = ['支付状态', 'financial_status'];
        $header[] = ['收货状态', 'fulfillment_status'];
        $header[] = ['标签', 'tags'];
        $tableName = '多渠道重复标签';
        $fileName = "export_" . $tableName . "_" . time();

        $data1['num'] = $num;
        table('order_source')
            ->where('date',$date)
            ->update($data1);
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }
    
}
